package com.hct.batterylife;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;





public class MainActivity extends Activity {

	ImageView img;
	Handler handler;
	TextView text;
	AnimationDrawable frameAnimation;
	Drawable[] drawableImg = new Drawable[17];
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		text= (TextView)findViewById(R.id.levelbattery);
		frameAnimation = new AnimationDrawable();
		startService(new Intent(this,BatteryService.class)); 
		
		for(int n =0,m=R.drawable.bigbattery_000 ; n <17; n++, m++)
		{
			drawableImg[n]= getResources().getDrawable(m);
		}

		handler = new Handler();

		handler.postDelayed(new Starter(),1000);

	}


	class Starter implements Runnable{

		@Override
		public void run() {
			img = (ImageView)findViewById(R.id.anim);
			int bLevel = BatteryService.getLevel();
			text.setText(bLevel+"%");
			int n=0;
			
			if(bLevel > 93) { n = drawableImg.length-1; }
			else if (bLevel > 87) { n = drawableImg.length-2; }
			else if (bLevel > 81) { n = drawableImg.length-3; }
			else if (bLevel > 75) { n = drawableImg.length-4; }
			else if (bLevel > 68) { n = drawableImg.length-5; }
			else if (bLevel > 62) { n = drawableImg.length-6; }
			else if (bLevel > 56) { n = drawableImg.length-7; }
			else if (bLevel > 50) { n = drawableImg.length-8; }
			else if (bLevel > 43) { n = drawableImg.length-9; }
			else if (bLevel > 37) { n = drawableImg.length-10; }
			else if (bLevel > 31) { n = drawableImg.length-11; }
			else if (bLevel > 25) { n = drawableImg.length-12; }
			else if (bLevel > 18) { n = drawableImg.length-13; }
			else if (bLevel > 12) { n = drawableImg.length-14; }
			else if (bLevel > 6) { n = drawableImg.length-15; }
			else {n = drawableImg.length-16;}
			for(; n>=0; n--)
				frameAnimation.addFrame(drawableImg[n], 150);
			
			frameAnimation.setOneShot(false);
			img.setBackgroundDrawable(frameAnimation);
			frameAnimation.start();
		}

	}


}
